#include "util.h"

inline void MemCopyAdvance(char **target, char **source, int64 bytes) {
  memcpy(*target, *source, bytes);
  *target += bytes;
  *source += bytes;
}

// For use with one reader and one writer.
// Can't write a chunk of more than the capacity.
// Spins the cpu on attempt to write when full. 
struct CircularBuffer {
public:
  CircularBuffer() { buffer_ = NULL; }
  void Init(int64 buffer_size);
  void Read(char ** target, int64 max_bytes);
  void Write(char *source, int64 bytes);
  int64 BytesReadable();
  int64 BytesWritable();
  void Display() {
    cout << "rp = " << (read_ptr_ - buffer_) << endl;
    cout << "wp = " << (write_ptr_ - buffer_) << endl;
  }

private:
    void ReadOnePart(char **target, int64 bytes);
  void WriteOnePart(char **source, int64 bytes);
  
  char * buffer_;
  char * buffer_end_;
  int64 buffer_size_;
  char * read_ptr_;
  char * write_ptr_;
};
